import requests
from bs4 import BeautifulSoup

#起始和下一链接
start_url = 'http://books.toscrape.com/'
next_url = start_url

while next_url:
#书籍详情页
    r = requests.get('http://books.toscrape.com/')
    r.raise_for_status()
    bs = BeautifulSoup(r.text, 'html.parser')
    ol = bs.find('ol', class_ = 'row')
    book_list = ol.find_all('li')
    for list in book_list:
        book_list = start_url + list.a['href']
        #print(book_list)

        book_now = requests.get(book_list)

        #对图书详情链接赋予变量
        bbs = BeautifulSoup(book_now.text, 'html.parser')
        book = {}
        #图书图片
        book['tp'] = bbs.find('div', id="product_gallery").img['src']
        #print(book) 测试正常

        #图书书名
        #book['name'] = bbs.find('div', class_='col-sm-6 product_main').h1
        #print(book) 测试正常，但不好进行下一步，通过导航栏可以直接定位书名和分类

        #导航栏
        breadcrumb = bbs.find('ul', class_="breadcrumb")
        #print(breadcrumb) 测试正常

        #书名
        book_name = breadcrumb.find('li', class_="active")
        book['name'] = book_name.text
        #print(book) 测试正常

        #分类
        #find_previous_sibling求同级类目上一节点
        book['fl'] = book_name.find_previous_sibling('li').text.strip()
        #print(book) 测试正常

        #价格
        book['jg'] = bbs.find('p', class_='price_color').text
        #print(book) 测试正常

        #库存
        book['kc'] = bbs.find('p', class_='instock availability').text.strip()
        #print(book) 测试正常

        #图书介绍
        book['js'] = bbs.find('div', class_="sub-header").find_next('p').text
        #print(book) 测试正常
        print(book)

    next_page = bs.find('li', class_='next')
    if not next_page:
        break
    next_url = start_url + next_page.a['href']
    print(next_url)